#!/bin/sh

if test "x$DEBUG_LAUNCHER" != x; then
    set -x
fi

# $0 is possibly relative path to this script.
SCRIPT_PATH="$( cd "$( dirname "$0" )" && pwd )"
PKGBASEPATH="$( cd "${SCRIPT_PATH}/../.." && pwd )"

echo "PKGBASE: $PKGBASEPATH"

APPLICATIONS_FONTFORGE="/Applications/FontForge.app"
if [ -L $APPLICATIONS_FONTFORGE ]; then
    echo "your fontforge install is using a link...";
    APPLICATIONS_FONTFORGE=$(readlink $APPLICATIONS_FONTFORGE);
    echo "you actually installed the real file into $APPLICATIONS_FONTFORGE"
fi


if [ y"$PKGBASEPATH" != "y$APPLICATIONS_FONTFORGE" ]; then
  COCOA_DIALOG="${SCRIPT_PATH}/CocoaDialog.app/Contents/MacOS/CocoaDialog";
  $COCOA_DIALOG msgbox --no-newline \
    --title "FontForge: Error starting" \
    --text "FontForge is in the wrong folder on your computer." \
    --informative-text "Please move the FontForge.app to your system /Applications folder." \
    --button1 "OK" --float
    BASEPATH="$( cd "${PKGBASEPATH}/.." && pwd )"
    open "$BASEPATH"
    exit;
fi

name=`basename "$0"`
tmp="$0"
tmp=`dirname "$tmp"`
tmp=`dirname "$tmp"`
bundle=`dirname "$tmp"`
bundle=/Applications/FontForge.app
scriptdir=/Applications/FontForge.app/Contents/MacOS/
bundle_contents="$bundle"/Contents
bundle_res="$bundle_contents"/Resources
bundle_fw="$bundle_contents"/Frameworks
bundle_lib="$bundle_res"/opt/local/lib
bundle_bin="$bundle_res"/opt/local/bin
bundle_data="$bundle_res"/opt/local/share
bundle_etc="$bundle_res"/opt/local/etc
bundle_share="$bundle_res"/opt/local/share

# export DYLD_LIBRARY_PATH="$bundle_lib"
export XDG_CONFIG_DIRS="$bundle_etc"/xdg
export XDG_DATA_DIRS="$bundle_data"
export GTK_DATA_PREFIX="$bundle_res"
export GTK_EXE_PREFIX="$bundle_res"
export GTK_PATH="$bundle_res"

export GTK2_RC_FILES="$bundle_etc/gtk-2.0/gtkrc"
export GTK_IM_MODULE_FILE="$bundle_etc/gtk-2.0/gtk.immodules"
export GDK_PIXBUF_MODULE_FILE="$bundle_etc/gtk-2.0/gdk-pixbuf.loaders"
export PANGO_RC_FILE="$bundle_etc/pango/pangorc"
export FONTCONFIG_FILE="$bundle_etc/fonts/fonts.conf"

export LANG=en_US.UTF-8
export XLOCALEDIR="$bundle_share/X11/locale"


WRAPPER=

if [ x"$1" == "x--debug" -o x"$1" == "x--debug-clearenv" ]; then

    if [ x"$1" == "x--debug-clearenv" ]; then
       # the user has instructed us to flush their environment
       echo "flushing some python related environment variables for this script..."
       unset PYTHONHOME
       unset LD_LIBRARY_PATH
       unset PYTHONPATH
       alias python=/usr/bin/python
       export PATH="/usr/bin:$PATH"
       echo "Also selected the default python executable."
       type -all python
    fi

    if command -v lldb 2>/dev/null; then

        export PYTHONPATH="/Applications/FontForge.app/Contents/MacOS/Even.app/Contents/Resources/lib/"
        export PATH="$PATH:$bundle_bin"
        export PYTHON="$bundle_bin/Python.framework.*/bin/python"
        export PYTHONHOME="$bundle_bin/Python.framework.2.7"

        echo "have set up python stuff... PYTHONHOME: $PYTHONHOME "
        echo "have set up python stuff... PYTHON:     $PYTHON "

        WRAPPER="gdb --args"
        WRAPPER="script $HOME/FontForge-Debug-Output.txt lldb --debug --source /Applications/FontForge.app/Contents/MacOS/debug-script -- "
        $WRAPPER
        # $WRAPPER $bundle_bin/fontforge "$2" "$3" "$4" "$5" "$6"
        exit
    else
        echo "You must installed the XCode Command Lines Tools to run FontForge"
        echo "in debug mode. For 10.7 they are available in the App Store. For"
        echo "10.6, install XCode from the App Store then install them by running"
        echo "XCode and looking in Preferences."
        exit
    fi
fi

# We lost Even.
if [1 = 0];
then
#
# only change these here to the local bundle ones so the scripts
# can run.
export PYTHONHOME="$bundle_bin/Python.framework.2.7"
export PYTHONPATH="/Applications/FontForge.app/Contents/MacOS/Even.app/Contents/Resources/lib/"
# This line allows python to find the compiled libraries so for example the gdraw
# module can use _libraries['libgdraw.so.5'] = CDLL('libgdraw.so.5')
export LD_LIBRARY_PATH="$bundle_lib"
fi;

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f FontForge.app
$scriptdir/fcprogress.pl 2>&1

# This ensures that File Open and File Save (etc) dialogs open in Home
# when FontForge is launched via normal OS X methods (double-clicking
# on the application or a SFD file, spotlight, etc.). If this script is
# invoked via command line, assume the user knows what they are doing,
# and don't change the current directory.
if [ "$( ps -o comm= $PPID)" = "/sbin/launchd" ] ; then
    cd ~/ || true
fi

# starting more than one does nothing, and it will close itself.
# /Applications/FontForge.app/Contents/MacOS/fontforge-crash-reporter.py &

export PATH="$PATH:$bundle_bin"
# echo $WRAPPER $bundle_bin/fontforge > /tmp/oo

if [ $# -eq 0 ] ; then
    ( exec $WRAPPER $bundle_bin/fontforge -new & )
else
    ( exec $WRAPPER $bundle_bin/fontforge "$@" & )
fi
